<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-header">
        <h2>万代校园管理系统</h2>
        <p>{{ userType === 'admin' ? '管理员登录' : '商户登录' }}</p>
      </div>
      
      <!-- 角色选择 -->
      <div class="role-selector">
        <el-radio-group v-model="userType" @change="handleRoleChange">
          <el-radio-button label="admin">管理员</el-radio-button>
          <el-radio-button label="merchant">商户</el-radio-button>
        </el-radio-group>
      </div>
      
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        class="login-form-content"
        @keyup.enter.native="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            size="large"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            size="large"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "LoginPage",
  data() {
    return {
      loading: false,
      userType: "admin", // 默认管理员登录
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, message: "密码长度不能少于6位", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    handleRoleChange() {
      // 清空表单
      this.loginForm = {
        username: "",
        password: ""
      }
      // 清除表单验证
      this.$nextTick(() => {
        this.$refs.loginForm && this.$refs.loginForm.clearValidate()
      })
    },
    
    async handleLogin() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true

        // 根据用户类型选择不同的登录接口
        const loginUrl = this.userType === 'admin' ? '/admin/login' : '/merchant/login'
        const loginData = {
          ...this.loginForm,
          userType: this.userType
        }

        const response = await this.$http.post(loginUrl, loginData)
        if (response.data.code === 200) {
          const { token, userInfo } = response.data.data
          
          // 保存token和用户信息
          localStorage.setItem("token", token)
          localStorage.setItem("userInfo", JSON.stringify(userInfo))
          localStorage.setItem("userType", this.userType)
          
          // 更新store
          this.$store.dispatch("login", { ...userInfo, userType: this.userType })
          
          this.$message.success("登录成功")
          
          // 根据用户类型跳转到不同页面
          if (this.userType === 'admin') {
            this.$router.push("/admin/dashboard")
          } else {
            this.$router.push("/merchant/labor-publish")
          }
        } else {
          this.$message.error(response.data.message || "登录失败")
        }
      } catch (error) {
        console.error("登录失败:", error)
        this.$message.error("登录失败，请检查用户名和密码")
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-form {
  width: 400px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.role-selector {
  margin-bottom: 30px;
  text-align: center;
}

.role-selector .el-radio-group {
  width: 100%;
}

.role-selector .el-radio-button {
  width: 50%;
}

.role-selector .el-radio-button__inner {
  width: 100%;
  border-radius: 4px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  margin-bottom: 10px;
  font-size: 24px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form-content {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  font-weight: 500;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  height: 45px;
}

.el-input__inner {
  height: 45px;
  line-height: 45px;
  font-size: 14px;
}
</style>
